const {
  http
} = require('../utils/request');
const {
  appId
} = require('../utils/config');
const WXBizDataCrypt = require('../utils/WXBizDataCrypt');

//  诉求列表
exports.appealList = async function (data = {}) {
  return await http.post('/wechat/appeal/list', {
    data: data
  })
}

//  诉求详情
exports.appealDetail = async function (data = {}) {
  return await http.post('/wechat/appeal/detail', {
    data: data
  })
}

//  提交诉求
exports.appealSubmit = async function (data = {}) {
  return await http.post('/wechat/appeal/submit', {
    data: data
  })
}

//  学子 注册
exports.studentRegister = async function (data = {}) {
  return await http.post('/wechat/student/register', {
    data: data
  })
}

//  添加教学经历
exports.createEducationalExperience = async function (data = {}) {
  return await http.post('/wechat/profile/createEducationalExperience', {
    data: data
  })
}

//  更新教育经历
exports.updateEducationalExperience = async function (data = {}) {
  return await http.post('/wechat/profile/updateEducationalExperience', {
    data: data
  })
}

//  删除教学经历
exports.deleteEducationalExperience = async function (data = {}) {
  return await http.post('/wechat/profile/deleteEducationalExperience', {
    data: data
  })
}


//  更新基本信息
exports.updateBasicInformation = async function (data = {}) {
  return await http.post('/wechat/profile/updateBasicInformation', {
    data: data
  })
}

//  反馈 列表
exports.feedbackList = async function (data = {}) {
  return await http.post('/wechat/feedback/list', {
    data: data
  })
}

//  反馈 提交反馈
exports.feedbackSubmit = async function (data = {}) {
  return await http.post('/wechat/feedback/submit', {
    data: data
  })
}

//  人才政策 列表
exports.talentPolicyList = async function (data = {}) {
  return await http.post('/wechat/talentPolicy/list', {
    data: data
  })
}

//  人才政策 详情
exports.talentPolicyDetail = async function (data = {}) {
  return await http.post('/wechat/talentPolicy/detail', {
    data: data
  })
}

//  热门活动收藏列表
exports.popularActivityCollectList = async function (data = {}) {
  return await http.post('/wechat/profile/popularActivityCollectList', {
    data: data
  })
}

//  人才政策收藏列表
exports.talentPolicyCollectList = async function (data = {}) {
  return await http.post('/wechat/profile/talentPolicyCollectList', {
    data: data
  })
}

//  联谊会收藏列表
exports.friendshipAssociationCollectList = async function (data = {}) {
  return await http.post('/wechat/profile/friendshipAssociationCollectList', {
    data: data
  })
}

//  获取 联谊会活动详情
exports.friendshipActivityDetail = async function (data = {}) {
  return await http.post('/wechat/friendshipActivity/detail', {
    data: data
  })
}

//	获取 联谊会活动 成员列表
exports.friendshipActivityMemberList = async function (data = {}) {
  return await http.post('/wechat/friendshipActivity/memberList', {
    data: data
  })
}


//  通过 openid 获取用户信息
exports.studentGet = async (data = {}) => {
  return await http.post('/wechat/student/get', {
    data: data
  })
}





//  获取字典项
exports.dictItemFind = async function (data = {}) {
  return await http.post('/wechat/dictItem/find', {
    data: data
  })
}

// 上传文件
exports.fileUpload = async function (file = {}) {
  return new Promise((resolve, reject) => {
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: filePreview('/file/upload'), // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      success(res) {
        //  对图片路径进行处理 使得可以进行预览
        const data = JSON.parse(res.data);
        data.url = filePreview(data.url);
        res.data = data;
        resolve(res);
      },
      fail(err) {
        reject(err);
      }
    });
  })
}

/** 
 * 文件预览
 * 前缀  /file/base64/ 、  /file/preview/
 */
function filePreview(url = '') {
  return http.base_url + url;
}

//  文件预览 ， 主要是进行文件路径的拼接
exports.filePreview = filePreview;

exports.filePreviews = (pro, str = '') => {

  //  对字符串进行分割
  const arr = str ? str.split(',') : [];
  return arr.map(_s => filePreview(pro + _s.trim()));
}

/**
 * 人脸识别 -> 判断当前设备是否支持人脸识别
 * @returns {
 * res.supportMode = [] 不具备任何被SOTER支持的生物识别方式
 * res.supportMode = ['fingerPrint'] 只支持指纹识别
 * res.supportMode = ['fingerPrint', 'facial'] 支持指纹识别和人脸识别
 * }
 */
exports.checkIsSupportSoterAuthentication = async function () {
  return new Promise((resolve, reject) => {
    wx.checkIsSupportSoterAuthentication({
      success(res) {
        // res.supportMode = [] 不具备任何被SOTER支持的生物识别方式
        // res.supportMode = ['fingerPrint'] 只支持指纹识别
        // res.supportMode = ['fingerPrint', 'facial'] 支持指纹识别和人脸识别
        resolve(res.supportMode);
      },
      fail: reject
    })
  })
}

/**
 * 开始人脸识别
 * @param name { string } 真实姓名
 * @param idCardNumber { string } 身份证号
 * @return bool
 */

exports.startFacialRecognitionVerify = async function (name, idCardNumber) {
  return new Promise((resolve, reject) => {
    wx.startFacialRecognitionVerify({
      name,
      idCardNumber,
      checkAliveType: 1, //屏幕闪烁(人脸核验的交互方式，默认0,读数字)
      success(res) {
        //  身份认证结果
        res.errCode == 0 ? resolve(true) : resolve(false);
      },
      fail: reject
    })
  })
}

//  获取用户基本信息
exports.getUserProfile = async () => {
  // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
  // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
  return new Promise((resolve, reject) => {
    wx.getUserProfile({
      desc: '获取基本信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        resolve(res.userInfo);
      },
      fail: reject
    })
  })
}

exports.getPhoneNumber = async (e) => {
  console.log(e)
  if (e.detail.errMsg == "getPhoneNumber:ok") {
    //需解密的事件中添加下边代码获取解密后的手机号码
    var sessionKey = wx.getStorageSync('session_key');
    var encryptedData = e.detail.encryptedData; //  手机密钥
    var iv = e.detail.iv;
    var pc = new WXBizDataCrypt(appId, sessionKey)
    var {
      phoneNumber
    } = pc.decryptData(encryptedData, iv) // 解密之后的手机号

    console.log('256', phoneNumber);
    wx.setStorageSync('phoneNumber', phoneNumber);
  } else {
    console.log("用户点击了拒绝")
  }

}

//  获取 access_token
exports.getAccessToken = async () => {

}


//  获取 教育经历
exports.educationalExperience = async function (data = {}) {
  return await http.post('/wechat/educationalExperience', {
    data: data
  })
}


//  档案信息
exports.profileArchiveInformation = async function (data = {}) {
  return await http.get('/wechat/profile/archiveInformation', {
    data: data
  })
}

//  创建档案信息查阅码
exports.profileBuildArchiveInformationCode = async function (data = {}) {
  return await http.get('/wechat/profile/buildArchiveInformationCode', {
    data: data
  })
}

//  查阅档案信息  - 根据查询码进行查询
exports.profileReferToArchiveInformation = async function (code = '') {
  return await http.get(`/wechat/profile/referToArchiveInformation/${ code }`, {})
}

//  文件上传.Base64
exports.uploadBase64 = async (data = {}) => {
  return await http.post('/file/uploadBase64', {
    data: data
  })
}

//  获取base64的值
exports.getBase64 = async (fid = '') => {
  return await http.get(`/file/base64/${ fid }`, {});
} 


// 查询高等学校
exports.collegeFind = async (data = {}) => {
  return await http.post('/wechat/college/find', {
    data: data
  })
}

//  文件下载
exports.downLoadFile = async (url = '') => {
  wx.downloadFile({
    url, //仅为示例，并非真实的资源
    success (res) {
      console.log(res);
      // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
      if (res.statusCode === 200) {}
    }
  })
}

//  获取用户授权信息
exports.requestSubscribeMessage = (arr = []) => {
  return new Promise((resolve, reject) => {
    wx.requestSubscribeMessage({   // 调起消息订阅界面
      tmplIds: arr,
      success (res) { 
        console.log('订阅消息 成功 ');
        resolve(res);
      },
      fail: reject
    }) 
  })
}